//
//  FiveBtnView.m
//  SHShopping
//
//  Created by 坤鼎 on 2019/8/30.
//  Copyright © 2019 kunding. All rights reserved.
//

#import "FiveBtnView.h"

#define ItemSpace    8

@interface FiveBtnView ()

@property (nonatomic, strong) BaseButton *btn;
@property (nonatomic, strong) BaseButton *btn0;
@property (nonatomic, strong) BaseButton *btn1;
@property (nonatomic, strong) BaseButton *btn2;
@property (nonatomic, strong) BaseButton *btn3;
@property (nonatomic, strong) BaseButton *btn4;

@end

@implementation FiveBtnView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UILabel *lbTitle = [[UILabel alloc] init];
        UILabel *lbDetailTitle = [[UILabel alloc] init];
        
        for (int i = 0; i < 5; i++) {
            BaseButton *btn = [[BaseButton alloc] init];
            btn.tag = 40 + i;
            btn.titleLabel.font = FONT_BOLD(16);
            btn.titleLabel.numberOfLines = 0;
//            btn.contentMode = UIViewContentModeScaleAspectFill;
//            btn.clipsToBounds = YES;
            [btn addTarget:self action:@selector(actionBtn:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
            
            switch (i) {
                case 0: {
                    self.btn0 = btn;
                }
                    break;
                case 1: {
                    self.btn1 = btn;
                }
                    break;
                case 2: {
                    self.btn2 = btn;
                }
                    break;
                case 3: {
                    self.btn3 = btn;
                }
                    break;
                case 4: {
                    self.btn4 = btn;
                }
                    break;
                default:
                    
                    break;
            }
        }
        lbTitle.frame = CGRectMake(LHYSpace, 0, SCREEN_WIDTH - 32, 31);
        lbDetailTitle.frame = CGRectMake(LHYSpace, GetViewYH(lbTitle) + ADAPT_H(6), SCREEN_WIDTH - 32, ADAPT_H(60));
        {
            CGFloat leftW = ADAPT_W(212.f);
            CGFloat leftH = leftW * 123.f/212.f;
            CGFloat threeW = ADAPT_W(109.f);
            
            self.btn0.frame = CGRectMake(LHYSpace, GetViewYH(lbDetailTitle) + ADAPT_H(15), leftW, leftH);
            self.btn1.frame = CGRectMake(SCREEN_WIDTH - LHYSpace - leftH, GetViewYH(lbDetailTitle) + ADAPT_H(15), leftH, leftH);
            self.btn2.frame = CGRectMake(LHYSpace, GetViewYH(self.btn0) + ADAPT_H(8), threeW, threeW);
            self.btn3.frame = CGRectMake(LHYSpace + ADAPT_W(8) + threeW, GetViewYH(self.btn0) + ADAPT_H(8), threeW, threeW);
            self.btn4.frame = CGRectMake(LHYSpace + (ADAPT_W(8) + threeW) * 2, GetViewYH(self.btn0) + ADAPT_H(8), threeW, threeW);
        }
        self.btn4.backgroundColor = UIColorFromRGB(0xEEEEEE);
        [self.btn4 setTitle:@"更多" forState:UIControlStateNormal];
        [self.btn4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
      
        [self addSubview:lbTitle];
        [self addSubview:lbDetailTitle];

        lbTitle.font = FONT_BOLD(22);
        lbDetailTitle.font = FONT(13);
        lbDetailTitle.textColor = UIColorFromRGB(0x555555);
        lbDetailTitle.numberOfLines = 0;
        
        self.lbTitle = lbTitle;
        self.lbDetailTitle = lbDetailTitle;
    }
    return self;
}

- (void)updateFiveBtnViewWith:(NSMutableArray *)modelArr {

    [self.btn0 sd_setBackgroundImageWithURL:[NSURL URLWithString:modelArr[0][@"imgPath"]] forState:UIControlStateNormal placeholderImage:LHYPlaceholderImage];
    [self.btn1 sd_setBackgroundImageWithURL:[NSURL URLWithString:modelArr[1][@"imgPath"]] forState:UIControlStateNormal placeholderImage:LHYPlaceholderImage];
    [self.btn2 sd_setBackgroundImageWithURL:[NSURL URLWithString:modelArr[2][@"imgPath"]] forState:UIControlStateNormal placeholderImage:LHYPlaceholderImage];
    [self.btn3 sd_setBackgroundImageWithURL:[NSURL URLWithString:modelArr[3][@"imgPath"]] forState:UIControlStateNormal placeholderImage:LHYPlaceholderImage];

    [self.btn0 setTitle:[NSString stringWithFormat:@"%@", modelArr[0][@"title"]] forState:UIControlStateNormal];
    [self.btn1 setTitle:[NSString stringWithFormat:@"%@", modelArr[1][@"title"]] forState:UIControlStateNormal];
    [self.btn2 setTitle:[NSString stringWithFormat:@"%@", modelArr[2][@"title"]] forState:UIControlStateNormal];
    [self.btn3 setTitle:[NSString stringWithFormat:@"%@", modelArr[3][@"title"]] forState:UIControlStateNormal];
}


- (void)actionBtn:(UIButton *)sender {
    if (self.clickRedeemBlock) {
        self.clickRedeemBlock(sender);
    }
}

@end
